First Game - Guess the Number
Well, we can't get too far into assembly without testing our skills. Here's the first "game" we'll make. And, in reality, we haven't quite learned enough to make a real game. This is going to be more of a simple tool. It's quite simple, doesn't need cute graphics, and will be a good check that you are understanding everything so far. The Program Plan The program works like this: it will ask the user to type a key, and then spit out the opcode for that button. Programming Pseudocode So, of course we need to start off with a more detailed outline, using "pseudocode," a basic code to help us know what we need to do. Clear the Screen Display "Press a key." Get the keypress as input Display "The key code for that key is: " Display the key code Step 1: Preparing the Screen So, first we want to get everything ready for the user. We'll clear the screen, and then type some text so the user knows what to do. Clearing the screen is easy with one of our favorite ROM Calls: b_call(_ClrLCDFull) ;clear screen Next, we need to define our text at the end of our program and label it. Let's have this data in bytes (.db) and label it "text1". Don't forget to zero-terminate it so it stops printing at the end. text1: .db "Press a key.",0 Then, we need to actually print the text. Instead of putting this in the corner, let's center it and move it down a bit. So, let's use our knowledge of Little-Endian processing to set CurCol and CurRow both at once. The display can fit 16 characters across and our text has 12, so let's leave 2 spaces on either side. To do this, let's set CurCol to 2, and then we can set CurRow to 1. Remember, CurCol comes right after CurRow, and HL will start with L first, so let's set HL to $0201. ld hl, $0201 ;H = 2, L = 1 ld (CurRow), hl ;L is used first, so CurRow = 1 ;H goes to the next byte in memory, so CurCol = 2 To print the text, we'll use our other ROM Call, _PutS. Remember, _PutS looks at HL to see where to start typing, so we better make sure HL holds the "text1" label. ld hl, text1 ;give HL the location of the text b_call(_PutS) ;print text starting at the location held by HL Step 2: Getting User Input Alright, so we got the screen all set up, and now the user is going to press a key. We need to know which is pressed, so let's use the ROM Call _GetKey. This will put the Key Code into the A register for us. b_call(_GetKey) ;now A holds the key code that was pressed Step 3: Printing the Results The last thing we need to do is print the key code out. First, however, we'll print the text "The key code for that key is ", so let's make another text label: text2: .db "The key code for that key is ",0 We'll also need to make sure our cursor is in the right place. Let's print it a couple lines down at row 4, column 0. ld hl, $0400 ;H = 4, L = 0 ld (CurRow), hl ;CurRow = 0, CurCol = 4 ld hl, text2 b_call(_PutS) ;print text2 And last of all, we'll use _DispHL to print it out. Our cursor is already where we want it: in the same place is stopped after we printed text2, so we don't need to move it this time. The key code is still in A, however, so let's copy A over to HL. ld h, 0 ;copy the keycode to HL ld l, a b_call(_DispHL) ;print the key code And there we go! The program is finished. However... ret Without executing this instruction, the program would not return to the TI Operating System, which means the program would never end. IMAGE OF RESULT The Full Program Here's the entire source code. b_call(_ClrLCDFull) ;clear screen ld hl, $0201 ;H = 2, L = 1 ld (CurRow), hl ;L is used first, so CurRow = 1 ;H goes to the next byte in memory, so CurCol = 2 ld hl, text1 ;give HL the location of the text b_call(_PutS) ;print text starting at the location held by HL b_call(_GetKey) ;now A holds the key code that was pressed ld hl, $0400 ;H = 4, L = 0 ld (CurRow), hl ;CurRow = 0, CurCol = 4 ld hl, text2 b_call(_PutS) ;print text2 ld h, 0 ;copy the keycode to HL ld l, a b_call(_DispHL) ;print the key code ret ;return to the TI OS text1: .db "Press a key.",0 text2: .db "The key code for that key is ",0